<template>
  <div id="UserManagePage">
    <a-form :model="searchParams" layout="inline" @finish="doSearch" style="padding-bottom: 20px">
      <a-form-item label="账号">
        <a-input v-model:value="searchParams.userAccount" placeholder="请输入账号" />
      </a-form-item>
      <a-form-item label="用户名">
        <a-input v-model:value="searchParams.userName" placeholder="请输入用户名" />
      </a-form-item>
      <a-button type="primary" htmlType="submit">搜索</a-button>
    </a-form>
    <a-table
      :columns="columns"
      :data-source="dataList"
      :pagination="pagination"
      @change="doTableChange"
      :isloading="loading"
    >
      <template #headerCell="{ column }">
        <template v-if="column.key === 'name'">
          <span>
            <smile-outlined />
            Name
          </span>
        </template>
      </template>

      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'userAvatar'">
          <a-image :src="record.userAvatar" :width="120"></a-image>
        </template>
        <template v-else-if="column.dataIndex === 'userRole'">
          <div class="user-role">
            <div v-if="record.userRole === 'admin'">
              <a-tag color="green">管理员</a-tag>
            </div>
            <div v-else-if="record.userRole === 'user'">
              <a-tag color="blue">普通用户</a-tag>
            </div>
            <div v-else>
              <a-tag color="red">封禁</a-tag>
            </div>
          </div>
        </template>
        <template v-else-if="column.dataIndex === 'createTime'">
          <span>{{ dayjs(record.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
        </template>
        <template v-else-if="column.key === 'action'">
          <a-popconfirm
            title="确定要删除用户吗？"
            @confirm="() => doDelete(record.id)"
            okText="确定"
            cancelText="取消"
          >
            <a-button danger>删除</a-button>
          </a-popconfirm>
        </template>
      </template>
    </a-table>
  </div>
</template>

<script lang="ts" setup>
import { deleteUserUsingPost, listUserVoPageUsingPost } from '@/api/userController'
import { SmileOutlined } from '@ant-design/icons-vue'
import dayjs from 'dayjs'
import { computed, onMounted, reactive, ref } from 'vue'
//列名称
const columns = [
  {
    title: 'id',
    dataIndex: 'id',
  },
  {
    title: '账号',
    dataIndex: 'userAccount',
  },
  {
    title: '用户名',
    dataIndex: 'userName',
  },
  {
    title: '头像',
    dataIndex: 'userAvatar',
  },
  {
    title: '简介',
    dataIndex: 'userProfile',
  },
  {
    title: '用户角色',
    dataIndex: 'userRole',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
  },
  {
    title: '操作',
    key: 'action',
  },
]

const dataList = ref([])
const total = ref()

//搜索条件
const searchParams = reactive<API.UserQueryRequest>({
  current: 1,
  pageSize: 10,
})

//分页参数
const pagination = computed(() => {
  return {
    current: searchParams.current,
    pageSize: searchParams.pageSize,
    total: total.value,
    showSizeChanger: true,
    showQuickJumper: true,
    showTotal: (total: number) => `共 ${total} 条`,
  }
})

//获取用户列表
const getUserList = async () => {
  const res = await listUserVoPageUsingPost(searchParams)
  console.log('获取用户列表', res)
  if (res.data.data && res.data.code === 0) {
    dataList.value = res.data.data.records
    total.value = res.data.data.total
  }
}

//切换表格页数
const doTableChange = (page: any) => {
  searchParams.current = page.current
  searchParams.pageSize = page.pageSize
  getUserList()
}
//删除用户
const doDelete = async (id: number) => {
  const res = await deleteUserUsingPost({ id })
  console.log('删除用户', res)
  if (res.data.code === 0 && res.data.data) {
    getUserList()
  }
}
//搜索
const doSearch = () => {
  searchParams.current = 1
  console.log('搜索', searchParams)
  getUserList()
}
onMounted(() => {
  getUserList()
})
</script>

<style lang="scss" scoped></style>
